<?php

class StoreModel extends Model {

    public function buystuff($uid = 0, $serialid = 0, $cnt = 0)
	{
        $uid = intval($uid);
        $serialid = $serialid;
        $cnt = intval($cnt);
        if (!$uid || !$serialid || !$cnt) return array('ecode' => ERR302);

		$userext = daocall("user", "getuserext", array($uid));
        if (!$userext || !is_array($userext)) return array('ecode' => ERR402);

		$sets = array();

		$conf = &load_conf("pub:stuff");
		$conf = &$conf['stuff'];
		if(!$conf || !$conf[$serialid]) return array('ecode' => ERR603);
		$conf = $conf[$serialid];
		
		$lv = intval(modcall("user", "xp2lv", array($userext["xp"])));
		if($lv < intval($conf["needlv"])) return array('ecode' => ERR405);
		if($userext["gb"] < intval($conf["gb"])) return array('ecode' => ERR404);
		if($userext["mb"] < intval($conf["mb"]) * $cnt) return array('ecode' => ERR403);

		if($userext["mb"])
		{
			$userext["mb"] -= $conf["mb"] * $cnt;
			$sets["mb"] = $userext["mb"];
		}

		if($userext["gb"])
		{
			$userext["gb"] -= $conf["gb"] * $cnt;
			$sets["gb"] = $userext["gb"];
		}

		if($sets && !daocall("user", "setuserext", array($uid, $sets))) return array('ecode' => ERR407);
        $stuff = &daocall("stuff", "getstuffs", array($uid));
		if(!daocall("stuff", "setstuff", array($uid, $serialid, intval($stuff[$serialid]["cnt"]) + $cnt))) return array('ecode' => ERR602);

		return array("ecode" => SUCCESS);
    }

	public function buyequip($uid = 0, $serialid = 0)
	{
        $uid = intval($uid);
        $serialid = $serialid;
        if (!$uid || !$serialid) return array('ecode' => ERR302);

		$userext = daocall("user", "getuserext", array($uid));
        if (!$userext || !is_array($userext)) return array('ecode' => ERR402);

		$sets = array();

		$conf = &load_conf("pub:equip");
		$conf = &$conf['equip'];
		if(!$conf || !$conf[$serialid]) return array('ecode' => ERR603);
		$conf = $conf[$serialid];
		
		$lv = intval(modcall("user", "xp2lv", array($userext["xp"])));
		if($lv < $conf["needlv"]) return array('ecode' => ERR405);
		if($userext["gb"] < intval($conf["gb"])) return array('ecode' => ERR404);
		if($userext["mb"] < intval($conf["mb"])) return array('ecode' => ERR403);

		if($userext["mb"])
		{
			$userext["mb"] -= $conf["mb"];
			$sets["mb"] = $userext["mb"];
		}

		if($userext["gb"])
		{
			$userext["gb"] -= $conf["gb"];
			$sets["gb"] = $userext["gb"];
		}

		if($sets && !daocall("user", "setuserext", array($uid, $sets))) return array('ecode' => ERR407);
		$eid = daocall("equip", "newequip", array($uid, array("etype" => $serialid)));
		if(!$eid) return array('ecode' => ERR604);

		return array("ecode" => SUCCESS, "ret" => $eid);
    }
	
}
?>